package com.ruoyi.consumer.controller;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.consumer.domain.CStatisticTable;
import com.ruoyi.consumer.service.ICStatisticTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: lizhouwei
 * @ date : 2024/3/21
 * @description:
 */

@Api(tags = "统计")
@RestController
@RequestMapping("/api/statistic")
public class StatisticController {

    @Autowired
    private ICStatisticTableService cStatisticTableService;

    @ApiOperation("统计卡片")
    @GetMapping("/queryStatisticCard")
    public AjaxResult queryStatisticCard(CStatisticTable cStatisticTable) {
        System.out.println("cStatisticTable = " + cStatisticTable);
        List<CStatisticTable> cStatisticTables = cStatisticTableService.selectCStatisticTableList(cStatisticTable);

        BigDecimal totalOrderMoney = BigDecimal.ZERO;
        BigDecimal totalOrderFilledKg = BigDecimal.ZERO; // 修改为BigDecimal类型
        int totalTransactions = cStatisticTables.size();

        for (CStatisticTable stat : cStatisticTables) {
            totalOrderMoney = totalOrderMoney.add(BigDecimal.valueOf(stat.getOrderMoney()));
            // 将克(g)转换为千克(kg)，并保留两位小数
            double filledKg = stat.getOrderFilled() / 1000.0;
            totalOrderFilledKg = totalOrderFilledKg.add(BigDecimal.valueOf(filledKg));
        }
        //将单位转为元保留两位小数
        totalOrderMoney = totalOrderMoney.divide(BigDecimal.valueOf(100),
                2, BigDecimal.ROUND_HALF_UP);

        // 格式化保留两位小数
        DecimalFormat df = new DecimalFormat("#.##");
        String totalOrderFilledKgFormatted = df.format(totalOrderFilledKg.doubleValue());

        Map<String, Object> result = new HashMap<>();
        result.put("totalOrderMoney", totalOrderMoney);
        result.put("totalOrderFilledKg", totalOrderFilledKgFormatted);
        result.put("totalTransactions", totalTransactions);

        return AjaxResult.success("统计成功", result);
    }

}
